package com.dm.yyxr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dm.yyxr.pojo.Admin;
import com.dm.yyxr.pojo.dto.AdminDto;
import com.dm.yyxr.pojo.dto.CommunityDto;
import com.dm.yyxr.pojo.dto.SupplyAndConsumptions;
import com.dm.yyxr.pojo.dto.SupplyAndConsumptionsDto;
import com.dm.yyxr.service.AdminService;
import com.dm.yyxr.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/admin")
@RestController
@CrossOrigin
public class AdminController {
    @Autowired
    AdminService adminService;

    @PostMapping("/supplyAndConsumptions")
    public Result<Object> supplyAndConsumptions(@RequestBody SupplyAndConsumptions supplyAndConsumptions){
        //根据供需信息查询管理员和社区信息
        SupplyAndConsumptionsDto supplyAndConsumptionsDto = adminService.getSupplyAndConsumptions(supplyAndConsumptions);
        return Result.ok(supplyAndConsumptionsDto);
    }
    //管理员登录
    @PostMapping("/login")
    public Result<Object> login(@RequestBody Admin admin){
        Admin byId = adminService.getByAdminName(admin.getAdmin_name());
        if(byId!=null && byId.getAdmin_passwd().equals(admin.getAdmin_passwd())){
            Map<String,Object> map = new HashMap<>();
            //String token = JwtUtil.getToken();
            map.put("token", UUID.randomUUID());
            map.put("admin",byId);
            return Result.ok(map);
        }else{
            return Result.fail().message("用户名或密码错误");
        }
    }
    //获取具体id的分局管理员的区域信息的分页列表
    @GetMapping("/getAreas/{id}")
    public Result<Object> getAreas(@PathVariable("id") Integer id,@RequestParam int pageNo){
        Page<CommunityDto> page = adminService.getCommunity(id,pageNo);
        return Result.ok(page);
    }
    //获取分局管理员的账号信息
    @GetMapping("/getAdmins")
    public Result<Object> getAdmin(@RequestParam Integer pageNo){
        Page<AdminDto> page = adminService.getAdmins(pageNo);
        return Result.ok(page);
    }
    //新增账号
    @PostMapping("/add")
    public Result<Object> add(@RequestBody Admin admin){
        adminService.save(admin);
        return Result.ok();
    }
    //修改账号
    @PostMapping("/update")
    public Result<Object> update(@RequestBody Admin admin){
        adminService.updateById(admin);
        return Result.ok();
    }
    //删除账号
    @GetMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable("id") Integer id){
        adminService.removeById(id);
        return Result.ok();
    }
}
